package com.ssm.sfzy.controller;

import com.ssm.sfzy.constant.CommonConstant;
import com.ssm.sfzy.entity.Good;
import com.ssm.sfzy.service.GoodService;
import com.ssm.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author song
 * @date 2024年04月01日 9:32
 * @version:1.0
 */
@RestController
@CrossOrigin
@RequestMapping("/good")
public class GoodController {
    @Autowired
    private GoodService goodService;

    @GetMapping("/find")
    public Object find(@RequestParam(defaultValue = CommonConstant.PAGE_NUM_DEFAULT) Integer pageNum,
                       @RequestParam(defaultValue = CommonConstant.PAGE_SIZE_DEFAULT) Integer pageSize,
                       Good good
                       ){
        Map<String, Object> map = goodService.findPage(pageNum, pageSize, good);
        return Result.success(map);
    }

    @GetMapping("/findById/{id}")
    public Object findById(@PathVariable Integer id){
        goodService.findById(id);
        return Result.success(id);
    }

    @PostMapping("/add")
    public Object add(@RequestBody Good good){
        System.out.println(good);
        goodService.add(good);
        return Result.success(good);
    }

    @PutMapping("/edit")
    public Object edit(@RequestBody Good good){
        System.out.println(good);
        goodService.edit(good);
        return Result.success(good);
    }

    @DeleteMapping("/remove/{id}")
    public Object remove(@PathVariable Integer id){
        goodService.remove(id);
        return Result.success(id);
    }

    @DeleteMapping("/removeBatch/{ids}")
    public Object removeBatch(Integer[] ids){
        goodService.removeBatch(ids);
        return Result.success(ids);
    }

}
